//
// Created by Administrator on 2025/9/15.
//

#ifndef F407_SDCARD_H
#define F407_SDCARD_H
#include "main.h"
extern SD_HandleTypeDef g_sd_handle;
extern HAL_SD_CardInfoTypeDef g_sd_card_info;
#define SD_TIMEOUT 1000

#define SDCARD_SDIO_D0_PORT GPIOC
#define SDCARD_SDIO_D1_PORT GPIOC
#define SDCARD_SDIO_D2_PORT GPIOC
#define SDCARD_SDIO_D3_PORT  GPIOC
#define SDCARD_SDIO_CLK_PORT GPIOC
#define SDCARD_SDIO_CMD_PORT GPIOD
#define SDCARD_SDIO_D0_PIN  GPIO_PIN_8
#define SDCARD_SDIO_D1_PIN  GPIO_PIN_9
#define SDCARD_SDIO_D2_PIN  GPIO_PIN_10
#define SDCARD_SDIO_D3_PIN  GPIO_PIN_11
#define SDCARD_SDIO_CLK_PIN  GPIO_PIN_12
#define SDCARD_SDIO_CMD_PIN  GPIO_PIN_2

#define SDCARD_SDIO_D0_GPIO_CLK_ENABLE __HAL_RCC_GPIOC_CLK_ENABLE
#define SDCARD_SDIO_D1_GPIO_CLK_ENABLE __HAL_RCC_GPIOC_CLK_ENABLE
#define SDCARD_SDIO_D2_GPIO_CLK_ENABLE __HAL_RCC_GPIOC_CLK_ENABLE
#define SDCARD_SDIO_D3_GPIO_CLK_ENABLE __HAL_RCC_GPIOC_CLK_ENABLE
#define SDCARD_SDIO_CLK_GPIO_CLK_ENABLE __HAL_RCC_GPIOC_CLK_ENABLE
#define SDCARD_SDIO_CMD_GPIO_CLK_ENABLE __HAL_RCC_GPIOD_CLK_ENABLE


uint8_t sdcard_init(void);

uint8_t sdcard_get_card_info(HAL_SD_CardInfoTypeDef *cardinfo);

uint8_t sdcard_get_card_sta(void);

uint8_t sdcard_read(uint8_t *buf,uint32_t saddr, uint32_t len);

uint8_t sdcard_write(uint8_t *buf,uint32_t saddr, uint32_t len);

uint8_t sdcard_read_dma(uint8_t *buf,uint32_t saddr, uint32_t len);

uint8_t sdcard_write_dma(uint8_t *buf,uint32_t saddr, uint32_t len);

volatile bool sdcard_get_tx_flag(void);

volatile bool sdcard_get_rx_flag(void);

uint8_t sdcard_test(void);

#endif //F407_SDCARD_H